//
//  XMGTabBar.m
//  百思不得姐
//
//  Created by Min on 16/5/23.
//  Copyright © 2016年 Min. All rights reserved.
/**
1. UIControl 来变量UITabBar，并且重新设置子控件的尺寸
2. tabBar的buttonClick方法里面 发送通知...
*/


#import "XMGTabBar.h"
#import "XMGPublishViewController.h"
#import "XMGPostWordViewController.h"
#import "XMGNavigationController.h"

@interface XMGTabBar()
/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
@end

@implementation XMGTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 设置tabbar的背景图片
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
        
        // 添加发布按钮
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        publishButton.size = publishButton.currentBackgroundImage.size;
        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:publishButton];
        self.publishButton = publishButton;
    }
    return self;
}
- (void)publishClick
{
    
    
    /** 1.0
//    XMGPublishViewController * publish = [[XMGPublishViewController alloc]init];
//    
//    // 使用跟控制器弹出窗体
//    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publish animated:NO completion:nil];
    
    // 窗口级别
    // UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
//    [XMGPublishView show];
    XMGPostWordViewController *postWord = [[XMGPostWordViewController alloc] init];
    XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:postWord];
    
    // 这里不能使用self来弹出其他控制器, 因为self执行了dismiss操作
    UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
    [root presentViewController:nav animated:YES completion:nil];
     */
    
    // 2.0
    XMGPublishViewController *publish = [[XMGPublishViewController alloc] init];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publish animated:NO completion:nil];
}
// 尺寸改变的时候，都会调用layoutSubviews方法。
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 标记按钮是否已经添加过监听器
    static BOOL added = NO;
    
    CGFloat width = self.width;
    CGFloat height = self.height;
    
    // 设置发布按钮的frame
    self.publishButton.center = CGPointMake(width * 0.5, height * 0.5);
    
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = width / 5;
    CGFloat buttonH = height;
    NSInteger index = 0;
    // 重新排布tabBar内部的按钮
    for (UIControl *button in self.subviews) {
        if (![button isKindOfClass:[UIControl class]] || button == self.publishButton) continue;
        
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 增加索引
        index++;
        
        if (added == NO) {
            // 监听按钮点击
            [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)buttonClick
{
    // 发出一个通知
    [XMGNoteCenter postNotificationName:XMGTabBarDidSelectNotification object:nil userInfo:nil];
}

@end
